package com.walk.zworld.ui.fragment

import android.content.Intent
import android.os.Build
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import com.jeremyliao.liveeventbus.LiveEventBus
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.mvvm.vm.HomeViewModel
import com.walk.zworld.ui.activity.LoginActivity
import com.walk.zworld.ui.adapter.HomeAdapter
import com.walk.zworld.ui.adapter.ImageAdapter
import com.walk.zworld.view.BannerView
import com.walk.zworld.view.banner.indicator.CircleIndicator
import com.walk.zworld.webView.WebViewActivity
import com.base.lib.base.utils.DensityUtil.dip2px
import com.base.lib.base.utils.MyMMKV.Companion.mmkv
import com.base.lib.base.utils.SettingUtil
import com.base.lib.base.view.BaseMvvmListFragment
import com.base.lib.httpUtils.Article
import com.base.lib.httpUtils.Banner

class HomeFragment : BaseMvvmListFragment<Article, HomeViewModel, HomeAdapter>() {

    companion object {
        fun newsInstance() = HomeFragment()
    }

    override fun onBindViewModel(): Class<HomeViewModel> = HomeViewModel::class.java
    override fun onBindAdapter(): Class<HomeAdapter> = HomeAdapter::class.java

    private lateinit var imgAdapter: ImageAdapter
    private var bannerView: BannerView<Banner, ImageAdapter>? = null

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView(view: View) {
        mAdapter.bindSkeletonScreen(
            mRecyclerView,
            R.layout.skeleton_item_home_list,
            8
        )
        bannerView = BannerView<Banner, ImageAdapter>(context)
        bannerView?.run {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dip2px(context, 230)
            )
            addBannerLifecycleObserver(this@HomeFragment)
            indicator = CircleIndicator(activity)
        }

        mAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                if (data.size != 0) {
                    val data = data[position]
                    WebViewActivity.start(activity, data.id, data.title, data.link)
                }
            }
            addHeaderView(bannerView as View)
            addChildClickViewIds(R.id.iv_like)
            setOnItemChildClickListener { adapter, view, position ->
                if (data.size == 0) return@setOnItemChildClickListener
                val res = data[position]
                when (view.id) {
                    R.id.iv_like -> {
                        if (!mmkv.decodeBool(Constant.IS_LOGIN, false)) {
                            startActivity(Intent(activity, LoginActivity::class.java))
                            return@setOnItemChildClickListener
                        }
                        val collect = res.collect
                        res.collect = !collect
                        setData(position, res)
                        if (collect) mViewModel.cancelCollectArticle(res.id).observe(requireActivity(), {})
                        else mViewModel.addCollectArticle(res.id).observe(requireActivity(), {})
                    }
                }
            }
        }
    }

    override fun initData() {
        LiveEventBus.get("refresh_homeBadge").observe(this) {

        }
//        showInitLoadView();
        isRefresh = true
        onRefreshEvent()
        initBanner()
    }

    private fun initBanner() {
        if (!SettingUtil.getIsShowBanner()) {
            bannerView?.visibility = View.GONE
            return
        }
        bannerView?.visibility = View.VISIBLE
        mViewModel.getBanner().observe(requireActivity()) {
            imgAdapter = ImageAdapter(requireActivity(), it)
            bannerView?.run {
                setAdapter(imgAdapter, true)
                setOnBannerListener { data, position ->
                    data as Banner
                    WebViewActivity.start(activity, data.id, data.title, data.url)
                }
            }
        }
    }

    private var homeBadge = 0
    private var homeBadgeOld = 0

    //初始化角标数据
    private fun initBadge(type: String, datas: Any) {
        when (type) {
            "home" -> {
                if (isRefresh)
                    homeBadge = 0
                for (i in 0 until (datas as MutableList<Article>).size) {
                    if (datas[i].fresh && datas[i].top != "1") {
                        homeBadge += 1
                    }
                }
                LiveEventBus.get("homeBadge").post(homeBadge)
                homeBadgeOld = homeBadge
            }
        }
    }

}